package com.zhbcm.socket.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2021/4/23 20:27
 */
public class SocketClientDemo
{
    public static void main(String[] args)
    {
        BufferedReader reader = null;
        PrintWriter writer = null;
        Socket client = null;
        try
        {
            client = new Socket("127.0.0.1", 18080);
            writer = new PrintWriter(client.getOutputStream());
            reader = new BufferedReader(new InputStreamReader(client.getInputStream()));

            while (true)
            {//每隔5秒发送一次请求
                writer.println("GET CURRENT TIME");
                writer.flush();
                String response = reader.readLine();
                System.out.println("Current Time:" + response);
                Thread.sleep(5000);
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                assert writer != null;
                writer.close();
                assert reader != null;
                reader.close();
                client.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}
